package com.fengye.algorithms.leecode;

/**
 * 外观数列
 * @Author fengye
 * @Date 2023/4/8 10:28
 **/
public class CountAndSay38 {
    public String countAndSay(int n) {
        if(n == 1) {
            return "1";
        }

        if(n == 2) {
            return "11";
        }

        if(n==3) {
            return "21";
        }

        StringBuilder sb = new StringBuilder();

        String temp = countAndSay(n -1) ;
        int num=1;
        for(int i=1;i<temp.length();i++) {
            if(temp.charAt(i) != temp.charAt(i-1)) {
                sb.append(num).append(temp.charAt(i-1));
                num=1;
                if(i == temp.length() - 1) {
                    sb.append(1).append(temp.charAt(i));
                }
            } else {
                num++;
                // 到最后一个特殊处理
                if (i == temp.length() - 1) {
                    sb.append(num).append(temp.charAt(i));
                }
            }

        }

        return sb.toString();
    }
}
